;(function ($, player) {
    function MusicPlayer(dom) {
        this.wrap = dom; //播放器的容器
        this.dataList = []; //请求到的播放数据
        this.indexObj = null;
        this.url = '../mock/data.json' //请求地址
        this.music = new player.music();
        this.rotateTimer = null;
        this.listTool = null;
        this.progress = player.progress.pro();
    }
    MusicPlayer.prototype = {
        init() {
            this.getDom();
            this.getData(this.url).then(data => {
                this.dataList = data;
                this.indexObj = new player.controlIndex(data.length);
                this.loadMusic(this.indexObj.index);
                this.listTool = player.listTool(data, document.getElementById('wrap'));
                this.musicControl();
                this.listToolCtr();
                this.dragProgress();
            })

        },
        getDom() {
            this.record = document.querySelector('.songImg img');
            this.controlBtns = document.querySelectorAll('.control li');

        },
        getData(url) {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: url,
                    method: 'get',
                    success: function (data) {
                        resolve(data);
                    },
                    error: function () {
                        reject('数据请求失败');
                    }
                });
            })

        },
        loadMusic(index,play) {
            player.render(this.dataList[index]) //渲染信息
            this.music.load(this.dataList[index].audioSrc); //加载音乐信息
            if (play) { //是否播放音乐
                this.music.play();
                this.progress.move(0);
                this.controlBtns[2].className='playing';
            }
            this.progress.renderAllTime(this.dataList[this.indexObj.index].duration);   
        },
        musicControl() {
            // 喜欢按钮
            this.controlBtns[0].addEventListener('touchend', () => {
                this.dataList[this.indexObj.index].isLike = !this.dataList[this.indexObj.index].isLike;
                player.render(this.dataList[this.indexObj.index]);
                //发送网络请求修改后台的数据。

            });
            // 前一首按钮
            this.controlBtns[1].addEventListener('touchend', () => {
                // console.log(this.indexObj.prev());
                this.loadMusic(this.indexObj.prev(),true);
              
            });
            //后一首按钮
            this.controlBtns[3].addEventListener('touchend', () => {
                this.loadMusic(this.indexObj.next(),true);
            });
            //暂停播放按钮
            this.controlBtns[2].addEventListener('touchend', () => {
                if (this.music.status == 'pause') {
                    this.music.play();
                    this.controlBtns[2].className = 'playing';
                    var deg = this.record.dataset.rotate || 0;
                    this.imgRotate(deg);
                    this.progress.move();
                } else {
                    this.music.pause();
                    this.controlBtns[2].className = '';
                    this.imgStop();
                    this.progress.stop();
                }

            });
            //显示播放列表
            this.controlBtns[4].addEventListener('touchend', () => {
                this.listTool.slideUp();
            });

        },
        imgRotate(deg) {
            clearInterval(this.rotateTimer);
            this.rotateTimer = setInterval(() => {
                deg = +deg + 0.6;
                this.record.style.transform = 'rotate(' + deg + 'deg)';
                this.record.dataset.rotate = deg;

            }, 1000 / 20);
        },
        imgStop() {
            clearInterval(this.rotateTimer);
        },
        listToolCtr(){
            this.listTool.musicList.forEach((item, index) => {
                item.addEventListener('touchend', () => {
                    if (this.indexObj.index != index) {
                        this.indexObj.index = index;
                    }
                    this.loadMusic(index,true);
                    this.listTool.slideDown();
                })
            })
        },
        dragProgress(){
			var circle=player.progress.drag(document.querySelector('.circle'));
			circle.init();
			circle.start=e=>{
               //按下事件
               cancelAnimationFrame(this.progress.frameId);
            }

			circle.move=per=>{
				//按下的时候
				this.progress.update(per);
			};

			circle.end=per=>{
                var cutTime=per*this.dataList[this.indexObj.index].duration;
				this.music.playTo(cutTime);
                this.progress.move(per);
				// player.music.play();
                if(this.music.status == 'play'){      
                    var deg=this.record.dataset.rotate || 0;
                    this.imgRotate(deg);	//旋转图片
                }else{
                    cancelAnimationFrame(this.progress.frameId);
                }
				

			}
		}
    }
    const wrap = document.getElementById('wrap');
    const music = new MusicPlayer(wrap);
    // player.progress.pro
    music.init()

})(window.Zepto, window.player);